﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Linyb.Linq.QueryExtension.Mapper;

namespace Linyb.Linq.QueryExtension
{
    public interface IPredicate
    {
        string GetSql(SqlConditionUtil util, IList<object> parameters);
    }

    public interface IBasePredicate : IPredicate
    {
        string PropertyName { get; set; }
    }

    public abstract class BasePredicate : IBasePredicate
    {
        public abstract string GetSql(SqlConditionUtil util, IList<object> parameters);
        public string PropertyName { get; set; }

        protected virtual string GetColumnName(Type entityType, SqlConditionUtil util, string propertyName)
        {
            IClassMapper map = util.Configuration.GetMap(entityType);
            if (map == null)
            {
                throw new NullReferenceException(string.Format("Map was not found for {0}", entityType));
            }

            IPropertyMap propertyMap = map.Properties.SingleOrDefault(p => p.Name == propertyName);
            if (propertyMap == null)
            {
                throw new NullReferenceException(string.Format("{0} was not found for {1}", propertyName, entityType));
            }

            //直接返回ColumnName
            return propertyMap.ColumnName;
        }
    }
}
